class Note {
  final int? id;
  final String title;
  final String content;
  final String? richContent; // 添加富文本内容字段，存储JSON格式的富文本数据
  final String category;
  final String tags;
  String? imageUrl; // 添加图片URL字段
  final int created_at;
  final int updated_at;

  Note({
    this.id,
    required this.title,
    required this.content,
    this.richContent, // 富文本内容可以为空
    this.category = '',
    this.tags = '',
    this.imageUrl, // 图片URL可以为空
    required this.created_at,
    required this.updated_at,
  });

  // 从Map创建Note对象
  factory Note.fromMap(Map<String, dynamic> map) {
    return Note(
      id: map['id']?.toInt(),
      title: map['title'] ?? '',
      content: map['content'] ?? '',
      richContent: map['rich_content'],
      category: map['category'] ?? '',
      tags: map['tags'] ?? '',
      imageUrl: map['image_url'],
      created_at: map['created_at']?.toInt() ?? DateTime.now().millisecondsSinceEpoch,
      updated_at: map['updated_at']?.toInt() ?? DateTime.now().millisecondsSinceEpoch,
    );
  }

  // 转换为Map
  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'title': title,
      'content': content,
      'rich_content': richContent,
      'category': category,
      'tags': tags,
      'image_url': imageUrl,
      'created_at': created_at,
      'updated_at': updated_at,
    };
  }

  // 复制并修改
  Note copyWith({
    int? id,
    String? title,
    String? content,
    String? richContent,
    String? category,
    String? tags,
    String? imageUrl,
    int? created_at,
    int? updated_at,
  }) {
    return Note(
      id: id ?? this.id,
      title: title ?? this.title,
      content: content ?? this.content,
      richContent: richContent ?? this.richContent,
      category: category ?? this.category,
      tags: tags ?? this.tags,
      imageUrl: imageUrl ?? this.imageUrl,
      created_at: created_at ?? this.created_at,
      updated_at: updated_at ?? this.updated_at,
    );
  }

  @override
  String toString() {
    return 'Note{id: $id, title: $title, category: $category}';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is Note && other.id == id;
  }

  @override
  int get hashCode => id.hashCode;
}